from Case.python_ui.Common.basepage import Basepage
import pytest, time
from Case.python_ui.PageLocate.user_my_resource_locator import UserMyResourceLocator
from Case.python_ui.PageLocate.product_management_locator import ProductManagementLocator
from Case.python_ui.PageLocate.user_product_waf_locator import ProductWafLocator
from Case.python_ui.Pageobject.system_setting_page import SystemSettingPage
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.Test_data.login_data import Logindata
from selenium import webdriver


class UserProductWaf(Basepage):

    # 进入My Resource界面
    def enter_my_resource_pages(self):
        time.sleep(2)
        doc = "进入user端的My Resource菜单"
        self.wait_elevisble(UserMyResourceLocator.my_resource_menu, doc)
        self.click_element(UserMyResourceLocator.my_resource_menu, doc)

    # 进入myresource页面
    def enter_my_resource_page(self):
        doc = '进入user端的myresource页面'
        for i in range(3):
            try:
                self.wait_elevisble(UserMyResourceLocator.my_resource_menu, doc)
                self.click_element(UserMyResourceLocator.my_resource_menu, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)

    def get_resouce_name_to_click(self,resource_name):
        doc = '获取到资源名称然后点击'
        resource_list = self.get_elements(ProductManagementLocator.resources, doc)
        for resource in resource_list:
            if resource.text == resource_name:
                resource.click()
        time.sleep(2)


    def get_prod_name_to_click(self,prod_name):
        doc = '获取产品名称然后点击'
        prod_list=self.get_elements(ProductManagementLocator.prod_list,doc)
        time.sleep(2)
        for prod in prod_list:
            if prod.text == prod_name:
                prod.click()
        time.sleep(2)

    # 进入My Resource中的Security页
    def enter_security_page(self):
        doc = "进入My Resource中的Security页"
        time.sleep(3)
        self.enter_my_resource_page()
        time.sleep(2)
        self.wait_elevisble(ProductWafLocator.security_page, doc)
        self.get_resouce_name_to_click("Security")
        time.sleep(3)

    # 进入WAF购买页
    def enter_bug_waf(self, page):
        doc = "进入WAF购买页"
        time.sleep(2)
        self.wait_elevisble(ProductWafLocator.waf_createnew_button, doc)
        self.move_mouse_to_element(ProductWafLocator.waf_createnew_button)
        time.sleep(2)
        self.get_prod_name_to_click('Web Application Firewall')
        time.sleep(5)
        self.switch_to_new_window()
        time.sleep(5)
        self.wait_elevisble(ProductWafLocator.billing_method_region_title, doc)
        time.sleep(2)
        content = self.get_element(ProductWafLocator.billing_method_region_title, doc).text
        self.save_screen(doc)
        assert content == page

    # 在WAF购买页点击domain的？，获取提示语
    def get_domain_tips(self,tips):
        doc = "在WAF购买页点击domain的？，获取提示语"
        time.sleep(2)
        self.wait_elevisble(ProductWafLocator.waf_help_button, doc)
        self.move_mouse_to_element(ProductWafLocator.waf_help_button)
        self.wait_elevisble(ProductWafLocator.waf_help_button_tips, doc)
        content = self.get_element(ProductWafLocator.waf_help_button_tips, doc).text
        assert content == tips

    # 创建一个Web Application Firewall产品购买配置
    def configure_waf_resource(self,method,region):
        doc = '创建一个Web Application Firewall'
        self.wait_elevisble(ProductWafLocator.billing_method_region_title, doc)
        # 获取默认的计费方式
        bmeth = self.get_element(ProductWafLocator.billing_method_button, doc).text
        assert bmeth == method
        # 获取默认的国家和地区
        regionz = self.get_element(ProductWafLocator.region_zone_key, doc).text
        assert regionz == region
        # 根据默认有效期获取产品费用
        ecosts = self.get_element(ProductWafLocator.estimated_costs_1month, doc).text
        return ecosts

    # 进入payment页面
    def enter_waf_paymentpage(self):
        doc = '进入payment页面'
        time.sleep(2)
        self.wait_elevisble(ProductWafLocator.bug_now_button, doc)
        self.click_element(ProductWafLocator.bug_now_button, doc)
        time.sleep(5)
        self.wait_elevisble(ProductWafLocator.waf_payment_page,doc)
        # 获取计算金额数据 并校验
        for i in range(2):
            try:
                self.wait_elevisble(ProductWafLocator.agree_bug_button, doc)
                self.move_mouse_to_click_element(ProductWafLocator.agree_bug_button, doc)
                break
            except:
                print('========:不存在购买协议按钮')
                time.sleep(1)
                continue
        pay = self.get_element(ProductWafLocator.waf_total_payment,doc).text
        time.sleep(1)
        self.wait_elevisble(ProductWafLocator.waf_pay_button,doc)
        self.click_element(ProductWafLocator.waf_pay_button,doc)
        time.sleep(8)
        # 检测是否跳转到支付成功页面
        self.wait_elevisble(ProductWafLocator.pay_susseccful_tips,doc)
        tips = self.get_element(ProductWafLocator.pay_susseccful_tips,doc).text
        print('++++++++++++++:',tips)
        self.save_screen(doc)
        assert tips == 'Payment Successful'
        return pay

    # 点击check order按钮,进入order页面
    def enter_waf_order(self):
        doc = '点击check order按钮,进入order页面'
        time.sleep(2)
        self.wait_elevisble(ProductWafLocator.waf_checkorder_button,doc)
        self.click_element(ProductWafLocator.waf_checkorder_button,doc)
        time.sleep(5)
        # 检测是否跳转到order页面
        self.wait_elevisble(ProductWafLocator.order_page, doc)
        ordpage_text = self.get_element(ProductWafLocator.order_page, doc).text
        assert ordpage_text == 'Orders'

    # 点击open console按钮,进入WAF控制台
    def enter_waf_console(self):
        doc = '点击open console按钮,进入WAF控制台'
        time.sleep(2)
        self.wait_elevisble(ProductWafLocator.waf_openconsole_button,doc)
        self.click_element(ProductWafLocator.waf_openconsole_button,doc)
        time.sleep(5)
        # 检测是否跳转到进入WAF控制台
        self.wait_elevisble(ProductWafLocator.waf_control_page, doc)
        wafpage_text = self.get_element(ProductWafLocator.waf_control_page, doc)
        assert wafpage_text == 'Web Application Firewall'

